//
//  ClassifyHelper.m
//  JZMCFM
//
//  Created by MJL on 15/10/31.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "ClassifyHelper.h"
#import "CategoryModel.h"

#import "CategoryDetailHeader.h"
#import "CategoryCellHeaderModel.h"
#import "CategoryDetailCellModel.h"

#import "CategoryPlayListModel.h"

static ClassifyHelper *share = nil;

@interface ClassifyHelper()<SDCycleScrollViewDelegate>
//用来存储分类数据的数组
@property(nonatomic,strong)NSMutableArray *dataArray;
//用来存储分类详情头部数据的数组
@property(nonatomic,strong)NSMutableArray *headerArray;
//用来存储分类详情cell数据的数组
@property(nonatomic,strong)NSMutableArray *cellDataArray;
//存储分类详情cell头部数组
@property(nonatomic,strong)NSMutableArray *cellHeaderArray;
//存储播放列表头部的数据
@property(nonatomic,strong)NSMutableArray *listHeaderArray;

@end

@implementation ClassifyHelper


+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[ClassifyHelper alloc]init];
    });
    return share;
}

#pragma mark 分类请求数据
- (void)downLoadDataFinished:(Result)result
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        //加载图片
        [SVProgressHUD showWithStatus:@"正在加载中" maskType:SVProgressHUDMaskTypeBlack];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        //获取数据
        [manager GET:CategoryUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //解析数据
            
            NSArray *listArray = responseObject[@"list"];
            for (NSDictionary *dict in listArray) {
                CategoryModel *model = [[CategoryModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                //回调
                result();
            });
            //数据加载成功,菊花停止
            [SVProgressHUD showSuccessWithStatus:@"加载成功" duration:2.0];
            //            NSLog(@"%@",self.dataArray);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
        
        
    });
}



#pragma mark 分类详情请求数据
- (void)downLoadHeaderDataWithString:(NSString *)string  withBlock:(Result)headerResult
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //加载图片
        [SVProgressHUD showWithStatus:@"正在加载中" maskType:SVProgressHUDMaskTypeBlack];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        //获取数据
        [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            //解析数据
            NSDictionary *focusImagesDict = responseObject[@"focusImages"];
            NSArray *listArray = focusImagesDict[@"list"];
            [self.headerArray removeAllObjects];
            for (NSDictionary *dict in listArray) {
                CategoryDetailHeader *model = [[CategoryDetailHeader alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                
                [self.headerArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                //回调
                headerResult();
            });
            //数据加载成功,小菊花停止
            [SVProgressHUD showSuccessWithStatus:@"加载成功" duration:2.0];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
    });
    
}


//#pragma mark cell数据请求
//- (void)downLoadCellDataWithString:(NSString *)string withBlock:(Result)cellResult
//{
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//
//
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//
//        NSArray *listArray = responseObject[@"list"];
//        [self.cellDataArray removeAllObjects];
//        for (NSDictionary *dict in listArray) {
//            CategoryDetailCellModel *model = [[CategoryDetailCellModel alloc]init];
//            [model setValuesForKeysWithDictionary:dict];
//            [self.cellDataArray addObject:model];
//        }
//        dispatch_async(dispatch_get_main_queue(), ^{
//            cellResult();
//        });
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//
//    }];
//
//    });
//}






#pragma mark 轮播图
- (UIView * )LBImageView:(NSArray *)ImageViewArr
{
    ImageViewArr  = [ClassifyHelper sharedInstance].categoryHeaderArray;
    NSMutableArray *imageURLAArray = [NSMutableArray array];
    for (int i = 0; i < ImageViewArr.count; i ++) {
        CategoryDetailHeader *model = ImageViewArr[i];
        [imageURLAArray addObject:model.pic];
    }
    SDCycleScrollView *cycleScrollView  = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, 375, 160) imagesGroup:nil];
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    cycleScrollView.delegate = self;
    cycleScrollView.dotColor = [UIColor brownColor];
    cycleScrollView.placeholderImage = [UIImage imageNamed:@"placeholder"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView.imageURLStringsGroup = imageURLAArray;
    });
    return cycleScrollView;
}


#pragma mark 自定义播放列表的头部
- (UIView *)setHeaderViewWithModel:(CategoryPlayListModel *)model
{
    CGFloat viewW = [UIScreen mainScreen].bounds.size.width;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewW, 180)];
    UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 80, 80)];
    [view addSubview:leftImageView];
    
    UIImageView *headerImage = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(leftImageView.frame) + 5, CGRectGetMinY(leftImageView.frame), 30, 30)];
    [view addSubview:headerImage];
    
    UILabel *nicknameLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headerImage.frame), CGRectGetMinY(headerImage.frame), 100, headerImage.frame.size.height)];
    [view addSubview:nicknameLabel];
    UILabel *introLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(headerImage.frame), CGRectGetMaxY(headerImage.frame) + 5, nicknameLabel.frame.size.width + 60, 35)];
    [view addSubview:introLabel];
    UILabel *cateLabel = [[UILabel alloc]initWithFrame:CGRectMake(introLabel.frame.origin.x, CGRectGetMaxY(introLabel.frame) + 3, 200, 30)];
    [view addSubview:cateLabel];
    return view;
    
}


#pragma amrk 解析播放列表头部
- (void)downLoadPlayLisetDataWithString:(NSString *)string withBlock:(Result)playBlock
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        //加载图片
        [SVProgressHUD showWithStatus:@"正在加载中" maskType:SVProgressHUDMaskTypeBlack];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        //获取数据
        [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //解析数据
            
            NSArray *listArray = responseObject[@"album"];
            for (NSDictionary *dict in listArray) {
                CategoryPlayListModel *model = [[CategoryPlayListModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [self.listHeaderArray addObject:model];
            }
            NSLog(@"%@",self.listHeaderArray);
            dispatch_async(dispatch_get_main_queue(), ^{
                //回调
                playBlock();
            });
            //数据加载成功,菊花停止
            [SVProgressHUD showSuccessWithStatus:@"加载成功" duration:2.0];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
        
        
    });
    
}


#pragma mark 懒加载
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray  = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSArray *)categoryArray
{
    return [self.dataArray copy];
}


- (NSMutableArray *)headerArray
{
    if (!_headerArray) {
        _headerArray = [NSMutableArray array];
    }
    return _headerArray;
}

- (NSArray *)categoryHeaderArray
{
    return [self.headerArray copy];
}



- (NSMutableArray *)cellHeaderArray
{
    if (!_cellHeaderArray) {
        _cellHeaderArray = [NSMutableArray array];
    }
    return _cellHeaderArray;
}

- (NSArray *)categoryCellHeaderArray
{
    return [self.cellHeaderArray copy];
}


- (NSMutableArray *)cellDataArray
{
    if (!_cellDataArray) {
        _cellDataArray = [NSMutableArray array];
    }
    return _cellDataArray;
}

- (NSArray *)categorycellArray
{
    return [self.cellDataArray copy];
}

- (NSMutableArray *)listHeaderArray
{
    if (!_listHeaderArray) {
        _listHeaderArray = [NSMutableArray array];
    }
    return _listHeaderArray;
}

@end
